package cn.xiongxf.service.impl;

import cn.xiongxf.domain.MessageSms;
import cn.xiongxf.domain.MessageSms;
import cn.xiongxf.dto.MessageStationDto;
import cn.xiongxf.mapper.MessageSmsMapper;
import cn.xiongxf.service.IMessageSmsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiongxiaofu
 * @since 2022-08-15
 */
@Service
public class MessageSmsServiceImpl extends ServiceImpl<MessageSmsMapper, MessageSms> implements IMessageSmsService {

    @Override
    public void save(String title, String content, String phone) {
        MessageSms messageSms = new MessageSms();
        messageSms.setTitle(title);
        messageSms.setContent(content);

        // 无需参数 直接获取IP地址
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        String remoteAddr = requestAttributes.getRequest().getRemoteAddr();
        messageSms.setIp(remoteAddr);

        messageSms.setSendTime(new Date());
//        messageSms.setUserId();  // TODO
        messageSms.setPhone(phone);
        insert(messageSms);
    }

    @Override
    public void saveMessageSms(MessageStationDto dto) {
        // 遍历，针对每个用户保存短信消息
        Date date = new Date();
        dto.getUserIds().forEach(userId -> {
            MessageSms messageSms = new MessageSms();
            messageSms.setTitle(dto.getTitle());
            messageSms.setContent(dto.getContent());
//            messageSms.setIp(dto.);  TODO
            messageSms.setSendTime(date);
            messageSms.setUserId(userId);
//          messageSms.setPhone(dto.);  feign调user服务查phone TODO
            insert(messageSms);
        });
    }
}
